var ws = null;

if ('WebSocket' in window == false) {
    alert("Current browser not support websocket!", 'danger')
}

window.onbeforeunload = function () {
    if (ws)ws.close();
}

function closeWebsocket() {
    if (ws)ws.close();
}

function createWebsocket() {
    current_host = window.location.host;
    url = 'ws://' + current_host + '/ws/video';
    console.log(url);
    ws = new WebSocket(url);
    ws.onerror = function () {
        console.log("websocket connect error");
    };
    ws.onopen = function () {
        console.log("websocket connect success");
    }
    ws.onmessage = function (event) {
        // value = JSON.parse(event.data);
        // console.log('websocket', value);

        let blob_data = dataURItoBlob(event.data);
        const src = URL.createObjectURL(blob_data);
        document.querySelector('img').src = src;
    }
    ws.onclose = function () {
        console.log("websocket connect close");
    }
}



var show_socket = new WebSocket("ws://127.0.0.1:8005/.json");
show_socket.onopen = function() {
    show_socket.send(JSON.stringify(["SUBSCRIBE", "video_capture"]));
};
show_socket.onmessage = function(evt)
{
    //  alert(evt.data);
    value = JSON.parse(evt.data);
    console.log(value);
    let blob_data = dataURItoBlob(value.SUBSCRIBE[2]);
    const src = URL.createObjectURL(blob_data);
    document.querySelector('img').src = src;
};


var control_socket = new WebSocket("ws://127.0.0.1:8005/.json");
control_socket.onopen = function() {
    control_socket.send(JSON.stringify(["SUBSCRIBE", "view_control"]));
};
control_socket.onmessage = function(evt)
{
    //  alert(evt.data);
    console.log(evt.data);
};